import time
import traceback

from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

driver = webdriver.Edge()


# XSS
def test():
    driver.get("http://10.90.42.223:9999/login")
    driver.maximize_window()
    wait = WebDriverWait(driver, 10)
    try:
        user_name = wait.until(
            EC.presence_of_element_located((By.XPATH, '/html/body/div[1]/div/div/div/form/div[1]/div/div/div/input'))
        )
        user_name.send_keys("<script>alert('xss')</script>")

        user_password = wait.until(
            EC.presence_of_element_located((By.XPATH, "/html/body/div[1]/div/div/div/form/div[2]/div/div/div/input"))
        )
        time.sleep(2)
        user_password.send_keys("123456")

        login_button = wait.until(
            EC.element_to_be_clickable((By.XPATH, "/html/body/div[1]/div/div/div/form/button"))
        )
        login_button.click()

        # 检查是否弹出alert对话框
        try:
            alert = WebDriverWait(driver, 5).until(EC.alert_is_present())
            alert_text = alert.text
            alert.accept()
            if alert_text == "xss":
                print("XSS漏洞存在！")
            else:
                print("未检测到XSS漏洞。")
        except:
            print("未检测到XSS漏洞。")
    except Exception as e:
        print(f"发生错误：{e}")
    finally:
        driver.quit()


if __name__ == '__main__':
    test()
